<?php 
include ('configuration.php'); 
	session_start();
	$var1= $_SESSION["registrado"];
	$var2= $_SESSION["administrador"];
	if ($var1 == null): {
		echo ("User not registered");
	} elseif ($var2 == 0): { 
			echo ("User without permissions");
	} elseif (empty($_POST['name_project']) && empty($_POST['datepicker'])&& empty($_POST['datepicker2'])):{
	    echo ("You can only access this page from new project and fields can´t be null");
	}else : {
?>


<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<link href="css/bootstrap.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.css" rel="stylesheet"	media="screen">

<title>My project manager</title>

<style type="text/css">@import url("css/calendar-blue.css");</style>
<script src="js/calendar.js" type="text/javascript"></script>
<script src="js/calendar-es.js" type="text/javascript"></script>
<script src="js/calendar-setup.js" type="text/javascript"></script>

<style type="text/css">
  body {
    padding-top: 60px;
    padding-bottom: 40px;
  }
  .sidebar-nav {
    padding: 9px 0;
  
  }
    .container {
    padding-top: 100px;
  }

    .finish {
    padding-top: 500px;
    padding-right: 30px;
  }
  

  
  @media (max-width: 980px) {
    /* Enable use of floated navbar text */
    .navbar-text.pull-right {
      float: none;
      padding-left: 5px;
      padding-right: 5px;
    }
  }
</style>

</head>

<body>

<script type="text/javascript" 	src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap.js"></script>

	<div class="span12">
	<div class="navbar navbar-inverse navbar-fixed-top">
		<div class="navbar-inner">
			<div class="container-fluid">
				<button type="button" class="btn btn-navbar" data-toggle="collapse"
					data-target=".nav-collapse">
					<span class="icon-bar"></span> <span class="icon-bar"></span> <span
						class="icon-bar"></span>
				</button>
				<a class="brand" href="http://localhost/bases--grupo10/homeAdmin.php?">My project manager</a>
				<div class="nav-collapse collapse">
					<p class="navbar-text pull-right">
					
						<a href="http://localhost/bases--grupo10/logout.php?" class="navbar-link">Log out</a>
					</p>
					<ul class="nav">
						<li class="active"><a href="http://localhost/bases--grupo10/homeAdmin.php?">Home</a></li>
						<li><a href="http://localhost/bases--grupo10/about.php?">About</a></li>
						<li><a href="http://localhost/bases--grupo10/contact.php?">Contact</a></li>
					</ul>
				</div>
			</div>
	    </div>
    </div>
</div>
	<?php
	//me traigo los datos del proyecto de la pantalla anterior
    $name_project=$_POST['name_project'];
    $fecha=$_POST['datepicker'];
    $fecha2=$_POST['datepicker2'];
    //compruebo que el nombre no sea demasiado largo
    if(strlen($name_project)>25) {
        ?><script type="text/javascript">
               alert("Name project is too long. Change name project");    
               history.back();
          </script>
        <?php
        //compruebo que las fechas no sean null 
    }else if (empty($name_project)){
        ?><script type="text/javascript">
               alert("Name project is null. Name project is required");    
               history.back();
          </script>
        <?php
    }else if(empty($fecha) || empty($fecha2)){
        ?><script type="text/javascript">
               alert("Some date is null. Initial and final date are required");    
               history.back();
          </script>
        <?php
        //compruebo que la fecha de inicio sea menor que la fecha de fin
    }else  if ( strtotime($fecha) > strtotime($fecha2)  ) {
    ?><script type="text/javascript">
                   alert("Initial date must be less than the final date");                 
                   history.back();
              </script>
           <?php  
    }else{  
        //compruebo que el nombre de proyecto no existe
        $q1=mysql_query("SELECT name_project FROM project WHERE name_project='$name_project'") or die ("Query error");
        $num_rows = mysql_num_rows($q1);
        if ($num_rows > 0) {
            ?><script type="text/javascript">
                       alert("Name project in use. Change the name project");    
                       history.back();
                  </script>
                <?php 
            }
            else{
                // si los datos son correctos, lo inserto 
                mysql_query("INSERT INTO project (name_project, id_version, deleted, advantage, initial_date, final_date)
                VALUES ('$name_project','1', '0', '0','$fecha' , '$fecha2')") or die('Error, query failed');
     
                ?><script type="text/javascript">
                     alert("Project created correct");
                  </script>
                 <?php                       
                 
            }                  
	?>
		
<div class="span12" >	
    <h1 class="muted"  >New project -> Add members</h1>
     <h3 class="muted" >Push Next if you don´t add members now</h3>
	 <h3 class="muted" >You can add members after in modify project</h3>				
</div>

<div class="span12">  
    <div class="container-fluid">	
       <div class="span5">
	 	  
	       <div class="control-group">
                <label class="control-label">Name project: <?php echo $name_project?> </label>
                <input id="name_project" name="name_project" type="hidden" value="<?php echo $name_project?>">
            </div>
    		
    		<div class="control-group">
    			<label class="control-label">Initial date: <?php echo $fecha?> </label>
    			 <input id="fecha" name="fecha" type="hidden" value="<?php echo $fecha?>">
            </div>

            <div class="control-group">
    			<label class="control-label">Final date: <?php echo $fecha2?> </label>
    			 <input id="fecha2" type="hidden" name="fecha2" value="<?php echo $fecha2?>">
            </div>
            
            <div class="control-group">
	            <label class="control-label">Advantage: 0</label>
             </div> 
	    </div>
	    
    	<?php
    	//consulta para saber el id del proyecto 
    	$q1 =  mysql_query ("SELECT id_project  FROM project WHERE name_project='$name_project'"); //ejecucion de la consulta
    	$q1_array = mysql_fetch_assoc($q1);	
    	$id_pro=$q1_array['id_project'];   
        ?>
        <div class="span5">
       		<div class="container-fluid" >
          	<form method="post" action="newProject_addResponsible.php?id_pro=<?php echo $id_pro?>">	
       		    <p>Select members with control+click to add to project and push Add members</p>
            	<select multiple="multiple" name="select_member[]" size="10">
                		<?php  
                		$QUERY =  mysql_query ("SELECT id_member, name_member, surnames_member  FROM project_member ORDER BY name_member asc"); //ejecucion de la consulta
                       	while ($row =mysql_fetch_array($QUERY))  {  
                           echo "<option value=".$row['id_member'].">".$row['name_member']." ".$row['surnames_member']. "</option>";
                       	}      
                       ?>    
                            
                   </select>
                   <input class="btn btn-primary" type="submit" value="Add members to project" >
               </form >
        	</div>
        </div> 
	    
      </div>
</div> 

<div class="finish">
    <a class="btn btn-primary pull-right" type="button"  href="http://localhost/bases--grupo10/homeAdmin.php?" >Next</a>
</div>

</body>
</html>




<?php   
}
       
}endif;?>

	